//https://gitee.com/api/v5/swagger#/getV5ReposOwnerRepoStargazers?ex=no
function blame(giteeparameter,callback) {
  let url = `https://gitee.com/api/v5/repos/${giteeparameter.owner}/${giteeparameter.repo}/contents/${giteeparameter.path}?access_token=${giteeparameter.access_token}`;
  $.get(url, function (data) {callback(data);});
}

function blame2(giteeparameter){
  let url = `https://gitee.com/api/v5/repos/${giteeparameter.owner}/${giteeparameter.repo}/contents/${giteeparameter.path}?access_token=${giteeparameter.access_token}`;
  return new Promise(function(resolve,reject){
    $.get(url, function (data) {resolve(data);});
  });
}

function gtreesha(access_token, owner, repo, path, ref,fun) {
    let url = `https://gitee.com/api/v5/repos/${owner}/${repo}/contents/${path}?access_token=${access_token}`;
    $.get(url, function (data) {
      let sha = data.sha;
      fun(sha);
    });
  }

function gtreesha2(giteeparameter){
  return new Promise(function(resolve,reject){
    let url = `https://gitee.com/api/v5/repos/${giteeparameter.owner}/${giteeparameter.repo}/contents/${giteeparameter.path}?access_token=${giteeparameter.access_token}`;
    $.get(url, function (data) {resolve(data);});
  });
}

/**
 * 
 * @param {*} access_token 
 * @param {*} owner 
 * @param {*} repo 
 * @param {*} path 
 * @param {*} message 
 * @param {*} content 
 */
function create(access_token,owner,repo,path,message,content){
  let url=`https://gitee.com/api/v5/repos/${owner}/${repo}/contents/${path}`
  $.post(url,{content:"",message:''},function(data){})
}

function createFile(giteeparameter,content,message,callback){
  let url=`https://gitee.com/api/v5/repos/${giteeparameter.owner}/${giteeparameter.repo}/contents/${giteeparameter.path}`
  $.post(url,{
    access_token:giteeparameter.access_token,
    content:content,
    message:message
  },function(data){callback(data)})
}

function createFile2(giteeparameter,content,message){
  let url=`https://gitee.com/api/v5/repos/${giteeparameter.owner}/${giteeparameter.repo}/contents/${giteeparameter.path}`
  return new Promise(function(resolve,reject){
    $.post(url,{
      access_token:giteeparameter.access_token, 
      content:content,
      message:message
    },function(data){resolve(data)})
  });
}

/** 
* 更新文件 
* @param  access_token
*/
function PutFile(giteeparameter,message,content){
  return new Promise(function(resolve,reject){
    $.ajax({
      url: `https://gitee.com/api/v5/repos/${giteeparameter.owner}/${giteeparameter.repo}/contents/${giteeparameter.path}`,
      type: "PUT",
      //dataType: "Content-Type: application/json;charset=UTF-8",
      //dataType: "json",
      data: {
        access_token:giteeparameter.access_token,
        sha: giteeparameter.sha,
        message: message,
        content: content,
      },
      success: function (response) {
        resolve(response)
      },
      error: function (xhr, status, error) {
        reject(error)
      },
    });
  })
}

function getreeput(access_token,owner,repo,path,sha,message,content,successfn,errorfn) {
    $.ajax({
      url: `https://gitee.com/api/v5/repos/${owner}/${repo}/contents/${path}`,
      type: "PUT",
      //dataType: "Content-Type: application/json;charset=UTF-8",
      //dataType: "json",
      data: {
        access_token:access_token,
        sha: sha,
        message: message,
        content: content,
      },
      success: function (response) {
        successfn(response);
        //console.log(response.message);
      },
      error: function (xhr, status, error) {
        errorfn(xhr,status,error)
        //console.log("Error: " + error);
      },
    });
}

//https://gitee.com/api/v5/swagger#/getV5ReposOwnerRepoGitBlobsSha

/** 
 * giteeBlob(access_token,owner,repo,sha,successfn)
 * 计算两个数的和并返回结果。
 * @param {number} access_token - 第一个加数。
 * @param {number} owner - 第二个加数。
 * @param {number} repo - 第二个加数。
 * @param {number} sha - 第二个加数。
 * @param {function name(params) {}} successfn - 第二个加数。
 * @returns {number} 两个数的和。
 */
function giteeBlob(access_token,owner,repo,sha,successfn) {
    //https://gitee.com/api/v5/repos/tuminghui/bookmark/git/blobs/function(data)%7B%20%20%20%20%20%20%20%20console.log(data);%20%20%20%20%20%20%7D?access_token=107fa6ac747e11e57536652d0662ebeb
    //https://gitee.com/api/v5/repos/tuminghui/bookmark/git/blobs/f2c81985a0c46c42a212aa65e3118ec596ee9e6c?access_token=107fa6ac747e11e57536652d0662ebeb
    let url = `https://gitee.com/api/v5/repos/${owner}/${repo}/git/blobs/${sha}?access_token=${access_token}`;
    //console.log(url);
    $.get(url, function (data) {
      successfn(data)
    })
}

function giteeBlob2(giteeparameter) {
  let url = `https://gitee.com/api/v5/repos/${giteeparameter.owner}/${giteeparameter.repo}/git/blobs/${giteeparameter.sha}?access_token=${giteeparameter.access_token}`;
  return new Promise(function(resolve,reject){
    $.get(url, function (data) {resolve(data);});
  });
}